package com.ml.mall.service.impl;

import com.ml.mall.bean.page.Page;
import com.ml.mall.bean.req.BaseDateRangeReq;
import com.ml.mall.bean.req.admin.category.AdminCategoryQueryOneReq;
import com.ml.mall.bean.req.admin.category.AdminCategoryQueryPageReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryOneReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQueryReportPageReq;
import com.ml.mall.bean.req.admin.purchase.PurchaseQuerySpentCategoryReq;
import com.ml.mall.bean.req.admin.variant.VariantQueryOneReq;
import com.ml.mall.bean.req.admin.variant.VariantQueryPageReq;
import com.ml.mall.bean.req.supplier.SupplierPurchaseQueryOneReportReq;
import com.ml.mall.bean.res.admin.PurchaseQueryOneRes;
import com.ml.mall.bean.res.admin.PurchaseReportPageRes;
import com.ml.mall.bean.res.admin.TypeSalesRes;
import com.ml.mall.bean.res.admin.category.AdminCategoryQueryPageRes;
import com.ml.mall.bean.res.admin.category.AdminCategoryQueryReportPageRes;
import com.ml.mall.bean.res.admin.variant.VariantQueryOneRes;
import com.ml.mall.bean.res.admin.variant.VariantQueryPageRes;
import com.ml.mall.bean.res.admin.variant.VariantQueryTopRes;
import com.ml.mall.bean.res.admin.vo.AdminOverviewSalesByCountryVO;
import com.ml.mall.bean.res.admin.vo.TypeSalesAndTotalSalesRes;
import com.ml.mall.bean.res.supplier.SupplierPurchaseQueryPageRes;
import com.ml.mall.bean.res.supplier.SupplierVariantQueryPageRes;
import com.ml.mall.bean.vo.DateValueVO;
import com.ml.mall.consts.OverviewTopConsts;
import com.ml.mall.consts.UserRoleTypeConsts;
import com.ml.mall.conversion.AnalyticsAdminServiceHelp;
import com.ml.mall.conversion.AnalyticsSupplierServiceHelp;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.AnalyticsAdminService;
import com.ml.mall.service.AnalyticsPartnersService;
import com.ml.mall.service.AnalyticsPurchaseService;
import com.ml.mall.service.AnalyticsSupplierService;
import com.ml.mall.service.asyn.AnalyticsAdminServiceAsyn;
import com.ml.mall.service.asyn.AnalyticsPurchaseServiceAsyn;
import com.ml.mall.service.asyn.AnalyticsSupplierServiceAsyn;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Future;

/**
 * Created by kyyang on 2021/7/3 14:44
 */
@Service
@Slf4j
public class AnalyticsSupplierServiceImpl implements AnalyticsSupplierService {


    @Autowired
    private AnalyticsSupplierServiceAsyn analyticsSupplierServiceAsyn;

    @Autowired
    private AnalyticsAdminServiceAsyn analyticsAdminServiceAsyn;

    @Autowired
    private AnalyticsAdminService analyticsAdminService;


    @Autowired
    private AnalyticsPurchaseService analyticsPurchaseService;

    @Override
    public Page<SupplierVariantQueryPageRes> supplierPurchaseCategoryQueryPage(PurchaseQuerySpentCategoryReq req) throws Exception {

        Page<SupplierVariantQueryPageRes> res = new Page<SupplierVariantQueryPageRes>(req.getPageIndex(),req.getPageNum());
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && (UserRoleTypeConsts.ROLE_TYPE_3 == currentUser.getRoleType() || UserRoleTypeConsts.ROLE_TYPE_1 == currentUser.getRoleType())) {
            Long supplierId = UserRoleTypeConsts.ROLE_TYPE_1 == currentUser.getRoleType() ? null : currentUser.getCompanyId();
            int count = 0;
            Future<Integer> countFuture = analyticsSupplierServiceAsyn.selectSupplierVariantQueryPageCount(req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), req.getCompanyId(), supplierId);
            if (countFuture != null && countFuture.get() != null) {
                count = countFuture.get();
            }
            res.setTotal(count);
            if (count > 0) {
                Future<List<SupplierVariantQueryPageRes>> purchaseReportPageResFuture = analyticsSupplierServiceAsyn.selectSupplierVariantQueryPageList(req.getTimeBegin(), req.getTimeEnd(), req.getSortType(), req.getCompanyId(), supplierId, req.getStart(), req.getEnd());
                if (purchaseReportPageResFuture != null) {
                    List<SupplierVariantQueryPageRes> purchaseReportPageRes = purchaseReportPageResFuture.get();
                    res.setRecords(purchaseReportPageRes);
                }
            }
        }
        return res;
    }

    @Override
    public TypeSalesAndTotalSalesRes supplierCategoryQueryTop(BaseDateRangeReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_3 == currentUser.getRoleType()) {
            TypeSalesAndTotalSalesRes typeSalesAndTotalSalesRes = analyticsAdminService.adminCategoryQueryTop(req, currentUser.getCompanyId());
            return typeSalesAndTotalSalesRes;
        }
        return new TypeSalesAndTotalSalesRes();
    }

    @Override
    public List<AdminOverviewSalesByCountryVO> supplierCountryQueryReport(BaseDateRangeReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_3 == currentUser.getRoleType()) {
            Future<List<AdminOverviewSalesByCountryVO>> adminOverviewSalesBySateVOFuture = analyticsAdminServiceAsyn.selectSalesByStatesNameTopN(req.getTimeBegin(),req.getTimeEnd(),currentUser.getCompanyId());
            if(adminOverviewSalesBySateVOFuture!=null){
                return adminOverviewSalesBySateVOFuture.get();
            }
        }
        return Collections.emptyList();
    }

    @Override
    public Page<AdminCategoryQueryReportPageRes> supplierCategoryQueryPage(AdminCategoryQueryPageReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && (UserRoleTypeConsts.ROLE_TYPE_3 == currentUser.getRoleType() || UserRoleTypeConsts.ROLE_TYPE_1 == currentUser.getRoleType())) {
            Long supplierId = UserRoleTypeConsts.ROLE_TYPE_1 == currentUser.getRoleType() ? null : currentUser.getCompanyId();
            Page<AdminCategoryQueryReportPageRes> backDate = analyticsAdminService.adminCategoryQueryReportPage(req, supplierId);
            return backDate;
        }else {
            Page<AdminCategoryQueryReportPageRes> backDate = analyticsPurchaseService.purchaseCategoryQueryReportPage(req,currentUser.getCompanyId());
            return backDate;
        }
    }

    @Override
    public List<DateValueVO> supplierCategoryQueryOne(AdminCategoryQueryOneReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_3 == currentUser.getRoleType()) {
            List<DateValueVO> adminCategoryQueryReportPageResPage = analyticsAdminService.adminCategoryQueryOne(req,currentUser.getCompanyId());
            return adminCategoryQueryReportPageResPage;
        }
        return Collections.emptyList();
    }

    @Override
    public Page<AdminCategoryQueryPageRes> supplierCategoryQueryOneSubsReport(AdminCategoryQueryPageReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_3 == currentUser.getRoleType().intValue()) {
            Page<AdminCategoryQueryPageRes> adminCategoryQueryReportPageResPage = analyticsAdminService.adminCategoryQueryPage(req,currentUser.getCompanyId());
            return adminCategoryQueryReportPageResPage;
        }else if(currentUser != null && UserRoleTypeConsts.ROLE_TYPE_1 == currentUser.getRoleType().intValue()){
            Page<AdminCategoryQueryPageRes> adminCategoryQueryReportPageResPage = analyticsAdminService.adminCategoryQueryPage(req,null);
            return adminCategoryQueryReportPageResPage;
        }
        return null;
    }

    @Override
    public List<VariantQueryTopRes> supplierVariantQueryTop(BaseDateRangeReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_3 == currentUser.getRoleType()) {
            List<VariantQueryTopRes> adminCategoryQueryReportPageResPage = analyticsAdminService.adminVariantQueryTop(req,currentUser.getCompanyId());
            return adminCategoryQueryReportPageResPage;
        }
        return Collections.emptyList();
    }

    @Override
    public Page<VariantQueryPageRes> supplierVariantQueryPage(VariantQueryPageReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_3 == currentUser.getRoleType()) {
            Page<VariantQueryPageRes> adminCategoryQueryReportPageResPage = analyticsAdminService.adminVariantQueryPage(req,currentUser.getCompanyId());
            return adminCategoryQueryReportPageResPage;
        }
        return null;
    }

    @Override
    public VariantQueryOneRes supplierVariantQueryOne(VariantQueryOneReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_3 == currentUser.getRoleType()) {
            VariantQueryOneRes adminCategoryQueryReportPageResPage = analyticsAdminService.adminVariantQueryOne(req,currentUser.getCompanyId());
            return adminCategoryQueryReportPageResPage;
        }
        return null;
    }

    @Override
    public PurchaseQueryOneRes supplierOrderQueryReport(BaseDateRangeReq reqt, Long companyId) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        //查询时间范围内 买家订单 订单数
        Future<List<DateValueVO>> purchaseOrderArrFuture =null;
        //查询时间范围内 买家订单花费金额
        Future<List<DateValueVO>> purchaseSpentArrFuture=null;
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_1 == currentUser.getRoleType()) {
            purchaseOrderArrFuture  = analyticsAdminServiceAsyn.selectPurchaseOrderByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), companyId,null);

            purchaseSpentArrFuture  = analyticsAdminServiceAsyn.selectPurchaseSpentByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), companyId,null);
        }else {
            purchaseOrderArrFuture  = analyticsAdminServiceAsyn.selectPurchaseOrderByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), companyId,currentUser.getCompanyId());

            purchaseSpentArrFuture  = analyticsAdminServiceAsyn.selectPurchaseSpentByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), companyId,currentUser.getCompanyId());
        }
        PurchaseQueryOneReq req = new PurchaseQueryOneReq();
        req.setTimeBegin(reqt.getTimeBegin());
        req.setTimeEnd(reqt.getTimeEnd());
        return AnalyticsAdminServiceHelp.adminPurchaseQueryOneDetailConversion(req, purchaseOrderArrFuture, purchaseSpentArrFuture);
    }

    @Override
    public List<SupplierPurchaseQueryPageRes> supplierPurchaseQueryTop(BaseDateRangeReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_1 == currentUser.getRoleType()) {
            //管理员查询全部
            Future<List<DateValueVO>> purchaseSpentArrFuture = analyticsAdminServiceAsyn.selectPurchaseSpentByTimeLine(req.getTimeBegin(), req.getTimeEnd(),null, null);
            return AnalyticsSupplierServiceHelp.supplierPurchaseQueryTopConversion(req,purchaseSpentArrFuture);
        }else {
            //查询时间范围内 买家订单花费金额
            Future<List<DateValueVO>> purchaseSpentArrFuture = analyticsSupplierServiceAsyn.selectPurchaseSpentByTimeLine(req.getTimeBegin(), req.getTimeEnd(),null, currentUser.getCompanyId());
            return AnalyticsSupplierServiceHelp.supplierPurchaseQueryTopConversion(req,purchaseSpentArrFuture);
        }
    }

    @Override
    public PurchaseQueryOneRes supplierPurchaseQueryOneReport(SupplierPurchaseQueryOneReportReq reqt) throws Exception {

        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_3 == currentUser.getRoleType()) {
            //查询时间范围内 买家订单 订单数
            Future<List<DateValueVO>> purchaseOrderArrFuture = analyticsSupplierServiceAsyn.selectPurchaseOrderByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), reqt.getCompanyId(), currentUser.getCompanyId());
            //查询时间范围内 买家订单花费金额
            Future<List<DateValueVO>> purchaseSpentArrFuture = analyticsSupplierServiceAsyn.selectPurchaseSpentByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), reqt.getCompanyId(), currentUser.getCompanyId());
            PurchaseQueryOneReq req = new PurchaseQueryOneReq();
            req.setTimeBegin(reqt.getTimeBegin());
            req.setTimeEnd(reqt.getTimeEnd());
            return AnalyticsAdminServiceHelp.adminPurchaseQueryOneDetailConversion(req, purchaseOrderArrFuture, purchaseSpentArrFuture);
        } else if(currentUser != null && UserRoleTypeConsts.ROLE_TYPE_1 == currentUser.getRoleType()){
            //查询时间范围内 买家订单 订单数
            Future<List<DateValueVO>> purchaseOrderArrFuture = analyticsAdminServiceAsyn.selectPurchaseOrderByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), reqt.getCompanyId(), currentUser.getCompanyId());
            //查询时间范围内 买家订单花费金额
            Future<List<DateValueVO>> purchaseSpentArrFuture = analyticsAdminServiceAsyn.selectPurchaseSpentByTimeLine(reqt.getTimeBegin(), reqt.getTimeEnd(), reqt.getCompanyId(), currentUser.getCompanyId());
            PurchaseQueryOneReq req = new PurchaseQueryOneReq();
            req.setTimeBegin(reqt.getTimeBegin());
            req.setTimeEnd(reqt.getTimeEnd());
            return AnalyticsAdminServiceHelp.adminPurchaseQueryOneDetailConversion(req, purchaseOrderArrFuture, purchaseSpentArrFuture);
        }
        return null;
    }

    @Override
    public Page<PurchaseReportPageRes> supplierPurchaseQueryPage(PurchaseQueryReportPageReq req) throws Exception {
        SysUser currentUser = JwtUtil.getCurrentUser();
        if (currentUser != null && UserRoleTypeConsts.ROLE_TYPE_1 == currentUser.getRoleType().intValue()) {
            Page<PurchaseReportPageRes> purchaseReportPageResPage = analyticsAdminService.adminPurchaseQueryReportPage(req,null);
            return purchaseReportPageResPage;
        }else {
            Page<PurchaseReportPageRes> purchaseReportPageResPage = analyticsAdminService.adminPurchaseQueryReportPage(req, currentUser.getCompanyId());
            return purchaseReportPageResPage;
        }
    }

}
